package com.dbq.fast.business.invite.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.invite.been.InviteInfo;
import com.dbq.fast.util.SystemUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanchen on 2018/5/4.
 */

public class ShareFragment extends DialogFragment {
    private String content;
    private String link;

    @BindView(R.id.content)
    TextView tvContent;

    public static ShareFragment newInstance(InviteInfo inviteInfo) {
        ShareFragment fragment = new ShareFragment();
        Bundle args = new Bundle();
        args.putString("content", inviteInfo.command);
        args.putString("link", inviteInfo.link);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * @param fragmentManager
     * @param inviteInfo      复制到粘贴板中的数据通过第二个参数传入
     */
    public static void showShareDialog(FragmentManager fragmentManager, InviteInfo inviteInfo) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ShareFragment prev = (ShareFragment) fragmentManager.findFragmentByTag("share");
        if (prev != null) {
            if (prev.isVisible()) {
                prev.dismiss();
            }
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        ShareFragment adDialogFragment = ShareFragment.newInstance(inviteInfo);
        try {
            adDialogFragment.show(ft, "share");
        } catch (Exception e) {
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        getDialog().setCanceledOnTouchOutside(false);// 点击外部不消失，点击X和返回键消失
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getDialog().getWindow().setDimAmount(0.45f);
        }
        getDialog().getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.share_fragment, null);
        ButterKnife.bind(this, view);

        content = getArguments().getString("content");
        link = getArguments().getString("link");
        tvContent.setText(content);
        return view;
    }

    @OnClick({R.id.share_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.share_btn:
                YibaAnalysis.getInstance().event(getActivity(), EventConstant.INVITE_PASTE);
                SystemUtil.copyToBoad(link);
                Toast.makeText(getActivity(), R.string.copy_to_board, Toast.LENGTH_LONG).show();
                this.dismiss();
                break;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }
}
